#include "Crossover.h"
#include "RandomNumberGenerator.h"

Crossover::Crossover(int numCrossovers)
{
    this->numCrossovers = numCrossovers;
}
Crossover::~Crossover() {
}

std::vector<Candidate*> Crossover::apply(Candidate* parent1, Candidate* parent2) {
    return this->mate(parent1,parent2);
}

 std::vector<Candidate*> Crossover::mate(Candidate* parent1, Candidate* parent2) {
    result.clear();
    Candidate* offspring1 = new Candidate();
    Candidate* offspring2 = new Candidate();
    offspring1->clone(parent1);
    offspring2->clone(parent2);
    for (int i = 0; i < numCrossovers; i++) {
       int crossoverIndex = (1 + RandomNumberGenerator::nextInt( parent1->getLength() -1));
       offspring1->swapSubString(offspring2,crossoverIndex);
    }
    result.push_back(offspring1);
    result.push_back(offspring2);
    return result;
 }
